using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text.RegularExpressions;


public class DataLoad
{
	public void LoadConfig()
	{
		//读取星球数据.
		LoadPlanet();
		//读取任务数据.
		LoadMission();
		//初始化存档.
		GameData.mainData.Resetup();
	}
	
	void LoadPlanet()
	{
		string[] text = LoadConfigFile("Planet");
		
		SortedList<int,PlanetData> List = new SortedList<int, PlanetData>();
		for(int i=1;i<text.Length;i++)
		{
			var PlanetData = new PlanetData();
			string[] Config = text[i].Split(',');
			if(Config.Length > 6)
			{
				PlanetData.Name = Config[1];
				PlanetData.BgName = Config[2];
				PlanetData.Shipyard = int.Parse(Config[3]);
				PlanetData.Government = int.Parse(Config[4]);
				PlanetData.Market = int.Parse(Config[5]);
				PlanetData.Bar = int.Parse(Config[6]);
				List.Add(int.Parse(Config[0]),PlanetData);
			}
		}
		GameData.PlanetDatas = List;
		Debug.Log("Planet-config load success");
	}
	
	public void LoadMission()
	{
		SortedList<int,MissionData> List = new SortedList<int, MissionData>();
		string[] loadText = LoadConfigFile("Mission");
		if(loadText != null)
		{
			for(int i=1;i<loadText.Length;i++)
			{
				var missionData = new MissionData();
				string[] Config = loadText[i].Split(',');
				if(Config.Length > 7)
				{
					missionData.Type = (MissionType)int.Parse(Config[1]);
					missionData.Name = Config[2];
					missionData.Describe = Config[3];
					if(Config[4] == "1")
						missionData.isLoop = true;
					else
						missionData.isLoop = false;
					int.TryParse(Config[5],out missionData.FollowMission);
					if(Config[6]!= "")
						missionData.PrestigeType = (PrestigeType)int.Parse(Config[6]);
					int.TryParse(Config[7],out missionData.PrestigeLevel);
					int.TryParse(Config[8],out missionData.Member);
					int.TryParse(Config[9],out missionData.Item);
					int.TryParse(Config[10],out missionData.Money);
					int.TryParse(Config[11],out missionData.AddPrestige);
					int.TryParse(Config[12],out missionData.AddMoney);
					int.TryParse(Config[13],out missionData.AddItem1);
					int.TryParse(Config[15],out missionData.AddItem2);
					int.TryParse(Config[14],out missionData.AddItem1Num);
					int.TryParse(Config[16],out missionData.AddItem2Num);
					int.TryParse(Config[17],out missionData.SpecialAwards);
					List.Add(int.Parse(Config[0]),missionData);
				}
			}
			GameData.MissionDatas = List;
			Debug.Log("Mission-config load success");
		}
	}
	
	string[] LoadConfigFile(string FileName){
		TextAsset text = (TextAsset)Resources.Load("Config/" + FileName,typeof(TextAsset));
		Regex re = new Regex(@"\r\n");
		string[] method2= re.Split(text.text);
		return method2;
	}
}